package org.openems.entity;


public class NumericEntity extends AbstractEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5001760096054484602L;

	private Double value = 0.0;
	private Integer decimal = 0;
	private Double min = 0.0;
	private Double max = 0.0;
	private Unit unit = Unit.none();
	
	@Override
	public boolean equals(Object obj) {
		NumericEntity newObj = (NumericEntity)obj;
		return this.decimal.equals(newObj.decimal) && 
				this.max.equals(newObj.max) && 
				this.min.equals(newObj.min) &&
				this.unit.equals(newObj.unit) &&
				this.value.equals(newObj.value);
	}

	public Double getMin() {
		return min;
	}

	public void setMin(Double min) {
		this.min = min;
	}

	public Double getMax() {
		return max;
	}

	public void setMax(Double max) {
		this.max = max;
	}

	public Unit getUnit() {
		return unit;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	@Override
	protected NumericEntity clone()  {
		try {
			return (NumericEntity)super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}

	public Double getValue() {
		return value;
	}

	public void setValue(Double value) {
		if (value >= min && value <= max) {
			this.value = value;
		}
		else {
			//throw new InvalidValueException(getKey(), min, max);
		}
	}

	public Integer getDecimal() {
		return decimal;
	}

	public void setDecimal(Integer decimal) {
		this.decimal = decimal;
//		fireEvent();
	}
	
	public NumericEntity(Key key, String name, Unit unit, Double min, Double max, boolean persisted) {
		super(key, name, persisted);
		this.unit = unit;
		this.min = min;
		this.max = max;
	}
	
	public NumericEntity(Key key, String name, int decimal, Unit unit, Double min, Double max, boolean persisted) {
		super(key, name, persisted);
		this.decimal = decimal;
		this.unit = unit;
		this.min = min;
		this.max = max;
	}

	@Override
	public String getValueText() {
		return String.format("%." + this.decimal.toString() +"f", this.value);
	}

	@Override
	public void setValueText(String text) throws InvalidValueException {
		Double value = Double.valueOf(text);
		this.setValue(value);
	}

	@Override
	public Entity getClone() {
		return this.clone();
	}
}
